package cn.mymall.service.mall.impl;

import cn.mymall.entity.mall.CartItem;
import cn.mymall.mapper.CartItemMapper;
import cn.mymall.service.impl.BaseServiceImpl;
import cn.mymall.service.mall.ICartItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CartItemServiceImpl extends BaseServiceImpl<CartItemMapper, CartItem> implements ICartItemService {

	@Override
	public List<CartItem> findByMemberId(Integer memberId) {
		return list(new QueryWrapper<CartItem>().eq("member_id", memberId));
	}

	@Override
	public Integer findCountByMemberId(Integer memberId) {
		QueryWrapper qw = new QueryWrapper<CartItem>().eq("member_id", memberId);
		qw.select("ifnull(sum(qty),0) as qty");
		Map resMap = getMap(qw);
		return MapUtils.getInteger(resMap, "qty");
	}

	@Override
	public CartItem findByMemberIdAndSkuId(Integer memberId, Integer skuId) {
		List<CartItem> list = list(new QueryWrapper<CartItem>().eq("member_id", memberId).eq("sku_id", skuId));
		if(CollectionUtils.isEmpty(list)){
			return null;
		}
		return list.get(0);
	}
}
